﻿using WebApplication1.Core.DataTransferObject;
using WebApplication1.Core.Domain.Entities;
using WebApplication1.Core.Domain.RepositoryContracts;
using WebApplication1.Core.ServiceContracts;

namespace WebApplication1.Core.Services {
    public class XiangxixinxiService : IXiangxixinxiService {

        private readonly IXiangxixinxiRepository _XiangxixinxiRepository;

        public XiangxixinxiService(IXiangxixinxiRepository XiangxixinxiRepository) {
            _XiangxixinxiRepository = XiangxixinxiRepository;
        }

        public async Task<XiangxixinxiResponse> AddXiangxixinxi(XiangxixinxiRequest? XiangxixinxiAddRequest) {
            if(XiangxixinxiAddRequest == null) {
                throw new ArgumentNullException(nameof(XiangxixinxiAddRequest));
            }
            Xiangxixinxi temp = XiangxixinxiAddRequest.ToXiangxixinxi();
            //temp.XiangxixinxiID = Guid.NewGuid();
            Xiangxixinxi addedXiangxixinxi = await _XiangxixinxiRepository.AddXiangxixinxi(temp);
            return addedXiangxixinxi.ToXiangxixinxiResponse();
        }

        public async Task<bool> DeleteXiangxixinxi(ushort? XiangxixinxiID) {
            if(XiangxixinxiID == null) { throw new ArgumentNullException(nameof(XiangxixinxiID)); }
            bool result = await _XiangxixinxiRepository.DeleteXiangxixinxi(XiangxixinxiID.Value);
            return result;
        }

        public async Task<IEnumerable<XiangxixinxiSimpleResponse>> GetAllXiangxixinxi() {
            IEnumerable<Xiangxixinxi> allXiangxixinxi = await _XiangxixinxiRepository.GetAllXiangxixinxi();
            return allXiangxixinxi.Select(x => new XiangxixinxiSimpleResponse() {
                Xiangmuid = x.Xiangmuid,
                XiangMuMingCheng = x.XiangMuMingCheng,
                ZhongBiaoDanWei = x.ZhongBiaoDanWei,
                JianLiDanWei = x.JianLiDanWei
            });
        }

        public async Task<IEnumerable<XiangxixinxiResponse>?> GetXiangxixinxiByMingCheng(string? mingCheng) {
            if(mingCheng == null) { throw new ArgumentNullException(nameof(mingCheng)); }
            IEnumerable<Xiangxixinxi> filterResult = await _XiangxixinxiRepository.GetFilteredXiangxixinxi(Xiangxixinxi => Xiangxixinxi.XiangMuMingCheng.Contains(mingCheng));
            return filterResult.Select(temp => temp.ToXiangxixinxiResponse());
        }

        public async Task<XiangxixinxiResponse?> GetXiangxixinxiByXiangxixinxiID(ushort? XiangxixinxiID) {
            if(XiangxixinxiID == null) { throw new ArgumentNullException(nameof(XiangxixinxiID)); }
            Xiangxixinxi? result = await _XiangxixinxiRepository.GetXiangxixinxiByXiangxixinxiID(XiangxixinxiID.Value);
            return result?.ToXiangxixinxiResponse();
        }

        public async Task<XiangxixinxiResponse?> UpdateXiangxixinxi(XiangxixinxiRequest? XiangxixinxiUpdateRequest) {
            if(XiangxixinxiUpdateRequest == null) { throw new ArgumentNullException(nameof(XiangxixinxiUpdateRequest)); }
            bool isSuccess = await _XiangxixinxiRepository.UpdateXiangxixinxi(XiangxixinxiUpdateRequest.ToXiangxixinxi());
            if(isSuccess) {
                return XiangxixinxiUpdateRequest.ToXiangxixinxiResponse();
            } else {
                return null;
            }
        }
    }
}
